package cn.kgc.controller;

import cn.kgc.common.R;
import cn.kgc.entity.Category;
import cn.kgc.entity.Employee;
import cn.kgc.service.CategoryService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     *新增 分类 套餐
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){

        categoryService.save(category);

        return R.success("新增完成");

    }

    /**
     * 分页
     */
    @GetMapping("/page")
    public  R<Page> page(int page ,int pageSize){
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

        //排序
        queryWrapper.orderByAsc(Category::getSort);
        //执行查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }

    /**
     * 删除
     */
    @DeleteMapping
    public R<String> delete(Long id){

        categoryService.remove(id);

        return R.success("分类信息删除成功");
    }


    /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){

        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }

    /**
     *  根据条件查询分类数据
     *
     */
@GetMapping("/list")
    public R<List<Category>> list(Category category){


    LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
    //根据条件查询 如果 Type 不等于空的话赋值
    queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());

    //添加排序条件   先跟sort sort一样的话按时间排序
    queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

    List<Category> list = categoryService.list(queryWrapper);

    return R.success(list);


    }
}
